package gemini.castor.ui.client.page.content.store.searchproduct.searchproductform;

import gemini.basic.dto.SearchProductCriteria;
import gemini.basic.model.Product;
import gemini.basic.model.ProductType;
import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.mvp.gin.CastorGinjector;
import gemini.castor.ui.client.page.content.store.shoppingcart.ShoppingCartEvent;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class SearchProductFormView extends Composite implements
		SearchProductFormPresenter.Display {

	@UiTemplate("SearchProductFormView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, SearchProductFormView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	private I18nConstants constants;

	public SearchProductFormView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
		htmError.setText(constants.stringRequireOnField());
	}

	@UiField
	RadioButton rdbByName;

	@UiField
	RadioButton rdbByCode;

	@UiField
	ListBox lbxCategory;

	@UiField
	TextBox tbxSearchKey;

	@UiField
	HTML htmError;

	@UiField
	FlexTable ftbResult;

	@UiField
	Button btnSearch;

	private SearchProductCriteria searchCriteria;

	private List<ProductType> productTypes;

	@Override
	public void setProductTypes(List<ProductType> productTypes) {
		this.productTypes = productTypes;
	}

	@Override
	public SearchProductCriteria getSearchCriteria() {
		return searchCriteria;
	}

	@Override
	public void loadData() {
		searchCriteria = new SearchProductCriteria();

		lbxCategory.clear();
		lbxCategory.addItem(constants.htmAll());

		for (int i = 0; i < productTypes.size(); i++) {
			lbxCategory.addItem(productTypes.get(i).getName());
		}
	}

	private void reset() {
		tbxSearchKey.removeStyleDependentName("error");
		htmError.setVisible(false);
	}

	@Override
	public boolean isValidate() {
		reset();
		boolean result = true;
		if (tbxSearchKey.getValue().isEmpty()) {
			result = false;
			showError();
		} else {
			searchCriteria.setByName(rdbByName.getValue());
			searchCriteria.setSearchKey(tbxSearchKey.getValue());
			if (lbxCategory.getSelectedIndex() != 0) {
				searchCriteria.setCategory(productTypes.get(
						lbxCategory.getSelectedIndex() - 1).getCode());
			}
		}
		return result;

	}

	private void showError() {
		tbxSearchKey.addStyleDependentName("error");

		htmError.setVisible(true);
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public HasClickHandlers getSearchClickHandlers() {
		return btnSearch;
	}

	@Override
	public void showResult(ArrayList<Product> products) {
		ftbResult.removeAllRows();

		if (products != null && !products.isEmpty()) {
			int i = 0;

			VerticalPanel pnlProductInfo;
			HorizontalPanel pnlButton;
			Button btnMoreDetail;
			Image img;
			String htmMoreDetail = constants.htmViewDetail();

			for (final Product pr : products) {
				img = new Image(pr.getImageUrl());
				img.setStyleName("gwt-Image-small");
				
				pnlProductInfo = new VerticalPanel();
				
				if(i > 0){
					pnlProductInfo.add(new HTML("<hr />"));
				}
				
				pnlProductInfo.add(img);
				pnlProductInfo.add(new HTML("<b>" + pr.getCode() + " - "
						+ pr.getName()
						+ "</b>"));
				pnlProductInfo.add(new HTML(pr.getProductDetail().getOverview()));

				btnMoreDetail = new Button(htmMoreDetail);
				btnMoreDetail.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						ShoppingCartEvent shopEvent = new ShoppingCartEvent(
								ShoppingCartEvent.ForwardType.VIEW_PRODUCT);
						shopEvent.setProduct(pr);
						CastorGinjector.INSTANCE.getEventBus().fireEvent(
								shopEvent);
					}
				});
				
				pnlButton = new HorizontalPanel();
				pnlButton.setWidth("100%");
				pnlButton.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
				pnlButton.add(btnMoreDetail);
				pnlButton.setStyleName("pnlButtonContainner");
				
				pnlProductInfo.add(pnlButton);

				ftbResult.setWidget(i, 0, pnlProductInfo);
				i++;
			}
		} else {
			htmError.setText(constants.htmNoResultFound());
			htmError.setVisible(true);
		}
	}
}
